import 'package:firebase_auth/firebase_auth.dart'
    hide EmailAuthProvider, AuthProvider;
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
import 'package:riverpod/riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:http/http.dart' as http;

import '../repositories/auth_repository.dart';
import '../repositories/custom_backend_auth_repository.dart';

part 'auth_provider.g.dart';

enum AuthProviderType { firebase, custom }

// * 登录方式,默认是自定义，可以切换为firebase
@Riverpod(keepAlive: true)
class AuthTypeNotifier extends _$AuthTypeNotifier {
  @override
  AuthProviderType build() {
    return AuthProviderType.custom;
  }

  void setFireBaseAuth() {
    state = AuthProviderType.firebase;
  }

  void setCustomAuth() {
    state = AuthProviderType.custom;
  }
}

// * firebase的auth登录方式
@Riverpod(keepAlive: true)
List<AuthProvider<AuthListener, AuthCredential>> authFirewareProviders(
    Ref ref) {
  return [
    EmailAuthProvider(),
  ];
}

// 提供一个 HTTP 客户端，用于后端请求
@Riverpod(keepAlive: true)
http.Client httpClient(Ref ref) {
  return http.Client();
}

// * 自定义后端的auth登录方式
@Riverpod(keepAlive: true)
CustomBackendAuthRepository customAuthProvider(Ref ref) {
  return CustomBackendAuthRepository(
      ref.watch(httpClientProvider), 'htphttps://your-backend-url.comps');
}

// * 模拟登录
@Riverpod(keepAlive: true)
AuthRepository authAppProvider(Ref ref) {
  return AuthRepository();
}

// * 后续可以尝试将 firebase 和 自定义后端的auth登录方式 统一起来
// @riverpod
// class AuthService extends _$AuthService {
//   @override
//   FutureOr<User?> build() async {
//     // 监听认证状态并返回当前用户
//     final authRepo = ref.watch(firebaseAuthRepositoryProvider); // 根据需要切换
//     return authRepo.currentUser;
//   }

//   Future<void> signOut() async {
//     final authRepo = ref.watch(firebaseAuthRepositoryProvider); // 根据需要切换
//     await authRepo.signOut();
//   }
// }
